package org.qiuqiu.backcontroller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.qiuqiu.service.impl.ReplyServiceImpl;
import org.qiuqiu.vo.PaginationVO;
import org.qiuqiu.vo.Reply;
import org.qiuqiu.vo.ReplyExample;
import org.qiuqiu.vo.User;
import org.qiuqiu.vo.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/***
 * 
 * @author wangsongmao
 *2015年2月4日
 * 上午9:28:29
 */
@Controller
@RequestMapping(value="/replyback")
public class ReplyBackController {
	@Autowired
	private ReplyServiceImpl replyServiceImpl;
	@RequestMapping("/replyback")
	public String replyback(){
		return "replyback";
		
	}
	@RequestMapping(value = "/replyList", method = RequestMethod.POST)
	public void getAllReply(
			@RequestParam(required = false, value = "key") String key,
			HttpServletResponse rs,
			HttpServletRequest re,
			@RequestParam(required = false, value = "pageIndex") int pageIndex,
			@RequestParam(required = false, value = "pageSize") int pageSize,
			@RequestParam(required = false, value = "sortField") String sortField,
			@RequestParam(required = false, value = "sortOrder") String sortOrder)
			throws Exception {
		List<Reply> replyList = null;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm")
				.create();
		PaginationVO page = new PaginationVO();
		page.setPageStart(pageIndex * pageSize);
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		Reply po = new Reply();
		ReplyExample poe = new ReplyExample();
		if (key != null) {
			po.setReplyUsername(key);
			poe.createCriteria().andReplyUsernameLike("%" + key + "%");
		}
		page.setSortField(sortField);
		page.setSortOrder(sortOrder);
		replyList = replyServiceImpl.selectPageByAll(page, po);
		int total = replyServiceImpl.countByExample(poe);
		System.out.println("" + replyList);
		page.setList(replyList);
		page.setTotalField(total);
		System.out.println(page);
		rs.setContentType("application/json; charset=utf-8");
		PrintWriter out = rs.getWriter();
		out.write(gson.toJson(page));
		out.flush();
		out.close();


}
}
